package com.fs.web.base.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.pojo.Company;
import com.fs.common.pojo.User;
import com.fs.web.base.annotation.SystemControllerLog;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.enums.OperationTypeEnum;
import com.fs.web.base.mapper.CompanyMapper;
import com.fs.web.base.mapper.UserMapper;
import com.fs.web.base.param.SysConstant;
import com.fs.web.base.service.ConsoleService;
import com.fs.web.base.service.LoginSerevice;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.vo.DataGrid;
import com.fs.web.base.vo.JsonResponse;
import com.fs.web.base.vo.LoginUser;
import com.fs.web.base.vo.SysPath;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author ACE
 * @desc 登录请求
 * @creationTime 2019-02-28 15:03
 * @projectName gps-root
 * @packageName com.fs.base.controller
 */
@Controller
@Slf4j
@RequestMapping("/console")
//@Api("登录相关api")
public class LoginController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginSerevice loginSerevice;
    @Autowired
    private ConsoleService consoleService;
    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 登录之后需要获取的数据
     */
    @ResponseBody
    @RequestMapping(value = "/data",method = RequestMethod.POST)
    public Object data(HttpServletRequest request){
        return null;
    }

    @GetMapping("/logout")
    @SystemControllerLog(description = "用户登出",type = OperationTypeEnum.SAVE)
    public String logout(HttpSession session){
        session.removeAttribute(SysConstant.SESSION_CONSOLE_USER);
        return SysPath.BACK_PATH.concat("/login");
    }

    @GetMapping("/login")
    public String loginGet(HttpSession session){
        return logout(session);
    }

    @GetMapping("")
    public String console(HttpServletRequest request, HttpSession session){
        return SysPath.BACK_PATH.concat("/console");
    }

    @GetMapping("/home")
    public String home(HttpServletRequest request){
        return SysPath.BACK_PATH.concat("/home");
    }

    @ApiOperation(value = "用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "帐号",required = true,dataType = "String"),
            @ApiImplicitParam(name = "passWord",value = "密码",required = true,dataType = "String")
    })
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @SystemControllerLog(description = "用户登录",type = OperationTypeEnum.SAVE)
    public JsonResponse login(HttpServletRequest request, HttpSession session){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            String account = map.get("username").toString();
            String pwd = map.get("password").toString();
            if(StringUtils.isBlank(account) || StringUtils.isBlank(pwd)){
                return JsonResponse.error(CodeEnum.EX105);
            }
            User user = new User(account,pwd);
            LoginUser uVo = loginSerevice.login(user);
            if(uVo.getLoginStatus()){
                List<Company> com = null;
                if(user.getAccount().equals(User.SYS_ADM)){
                    com = companyMapper.selectList(null);
                }else {
                    QueryWrapper<Company> wrapper = new QueryWrapper<>();
                    wrapper.like("line",uVo.getUser().getComId());
                    com = companyMapper.selectList(wrapper);
                }
                session.setAttribute(SysConstant.SESSION_CONSOLE_USER, uVo);
                session.setAttribute(SysConstant.SESSION_CONSOLE_MENU, JSON.toJSON(DataGrid.treeBuilder(uVo.getMenus(), "id", "parentId")).toString());
                session.setAttribute(SysConstant.SESSION_CONSOLE_MENU_LIST, uVo.getMenus());
                session.setAttribute(SysConstant.SESSION_CONSOLE_MENU_MOTION, JSON.toJSON(uVo.getMotions()).toString());
                session.setAttribute(SysConstant.SESSION_COMPANY, com);
                session.setAttribute(SysConstant.BASICSCONFIG,consoleService.getConfig());
            }else {
                return JsonResponse.error(uVo.getLoginResult());
            }
            return JsonResponse.success(uVo);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

}
